<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\library\Ems;
use app\common\library\Sms;
use fast\Random;
use think\Config;
use think\Validate;
use think\Db;

/**
 * 道具接口
 */
class Prop extends Api
{
    protected $noNeedLogin = ['getPropType','getPropInfo'];
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();
        if (!Config::get('fastadmin.usercenter')) {
            $this->error(__('User center already closed'));
        }
    }

    /**
     * 道具分类
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0",description="通讯状态，0为失败，1为成功")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功",description="返回内容")
     * @ApiReturnParams   (name="data",description="扩展数据返回")
     * @ApiReturnParams   (name="id", type="string", required=true, sample="返回成功",description="分类id")
     * @ApiReturnParams   (name="name", type="string", required=true, sample="返回成功",description="分类中文名")
     * @ApiReturnParams   (name="eng_name", type="string", required=true, sample="返回成功",description="分类英文名")
     */
    public function getPropType()
    {
        if($this->auth->id){
            $user = DB::name("user")->where("id",$this->auth->id)->find();
            $lang_type = $user['lang_type'];
        }else{
            $lang_type = 1;
        }
        $result = DB::name("prop_type")->order("weigh desc")->field("id,name,eng_name")->select();
        $this->success('查询成功', $result);
    }

    
    /**
     * 道具列表
     * @param string $type  查询类型：1=热门查询,2=全部查询,3=普通查询
     * @param string $prop_type_id  道具分类id，Type为3时必传
     * @param string $page  页码，默认为1
     * @param string $page_size  每页数量，默认15
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0",description="通讯状态，0为失败，1为成功")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功",description="返回内容")
     * @ApiReturnParams   (name="data",description="扩展数据返回")
     * @ApiReturnParams   (name="id", type="string", required=true, sample="返回成功",description="道具id")
     * @ApiReturnParams   (name="name", type="string", required=true, sample="返回成功",description="道具名")
     * @ApiReturnParams   (name="image", type="string", required=true, sample="返回成功",description="道具图片")
     * @ApiReturnParams   (name="money", type="string", required=true, sample="返回成功",description="道具价格")
     * @ApiReturnParams   (name="blurb", type="string", required=true, sample="返回成功",description="道具简介")
     * @ApiReturnParams   (name="num", type="string", required=true, sample="返回成功",description="已售数量")
     * @ApiReturnParams   (name="type", type="string", required=true, sample="返回成功",description="道具类型:1=普通道具,2=改名卡,3=章节解锁卡,4=全章节解锁卡,5=抵扣券,6=特权卡,7=实体道具,8=全服喇叭")
     * @ApiReturnParams   (name="episode_script_id", type="string", required=true, sample="返回成功",description="解锁剧本")
     * @ApiReturnParams   (name="episode_chapter_id", type="string", required=true, sample="返回成功",description="解锁剧集")
     * @ApiReturnParams   (name="lang_type", type="string", required=true, sample="返回成功",description="语言版本:1=中文版,2=英文版")
     * @ApiReturnParams   (name="buycar_num", type="string", required=true, sample="返回成功",description="购物车商品数量")
     */
    public function getPropInfo()
    {
        if($this->auth->id){
            $user = DB::name("user")->where("id",$this->auth->id)->find();
            $lang_type = $user['lang_type'];
        }else{
            $lang_type = 1;
        }

        $where = array("lang_type"=>$lang_type);
        $type = input("type");
        if(empty($type)){
            $this->error("请选择查询类型");
        }
        if($type != 1 && $type != 2 && $type != 3){
            $this->error("查询类型有误");
        }
        $sort = "weigh desc";
        if($type == 1){
            $sort = "num desc";
        }else if($type == 3){
            $prop_type_id = input("prop_type_id");
            if(empty($prop_type_id)){
                $this->error("请选择道具分类");
            }
            $where['prop_type_id'] = $prop_type_id;
        }
        $page = input("page")?input("page"):1;
        $page_size = input("page_size")?input("page_size"):15;
        $list = DB::name("system_prop")->where($where)->order($sort)->page($page,$page_size)->field("id,name,image,money,blurb,num,type,entity_type,episode_script_id,episode_chapter_id,lang_type")->select();
        
        foreach($list as $key => $val){
            if($this->auth->id){
                /*
                 * 1.用户购买过，或者用户使用过的话 can_buy = 0
                 * 2.用户既没有购买过，也没有使用过的话，can_buy = 1
                 */
                $user_bag = DB::name("user_bag")->where(array("user_id"=>$this->auth->id,"system_prop_id"=>$val['id']))->find();
                if(!empty($user_bag)){
                    $list[$key]['can_buy'] = 0;
                }else {
                    $list[$key]['can_buy'] = 1;
                }
                
                $buycar_num = DB::name("user_buycar")->where(array("user_id"=>$this->auth->id,"system_prop_id"=>$val['id']))->find();
                if($buycar_num){
                    $list[$key]['buycar_num'] = $buycar_num['num'];
                }else{
                    $list[$key]['buycar_num'] = 0;
                }
                $result['buycar_num'] = $buycar_num;
            }else{
                $list[$key]['buycar_num'] = 0;
                $result['buycar_num'] = 0;
            }
        }
        
        $result['list'] = $list;
        $result['total'] = DB::name("system_prop")->where($where)->count();

        if($this->auth->id){
            $buycar_num = DB::name("user_buycar")->where(array("user_id"=>$this->auth->id,"lang_type"=>$lang_type))->sum("num");
            $result['buycar_num'] = $buycar_num;
        }else{
            $result['buycar_num'] = 0;
        }

        $this->success('查询成功', $result);
    }

    /**
     * 加入/删除购物车
     * @ApiMethod (POST)
     * @param string $prop_info_id  道具id
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0",description="通讯状态，0为失败，1为成功")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功",description="返回内容")
     * @ApiReturnParams   (name="data",description="扩展数据返回")
     */
    public function doAddUserBuycar()
    {
        $prop_info_id = input("prop_info_id");
        if(empty($prop_info_id)){
            $this->error("请选择道具");
        }
        $prop_info = DB::name("system_prop")->where("id",$prop_info_id)->find();
        if(empty($prop_info)){
            $this->error("道具有误");
        }

        $user_buy_car = DB::name("user_buycar")->where(array("user_id"=>$this->auth->id,"system_prop_id"=>$prop_info_id))->find();
        if($user_buy_car){
            DB::name("user_buycar")->where("id",$user_buy_car['id'])->delete();
            $msg = "删除购物车成功";
        }else{
            $data = array(
                "user_id" => $this->auth->id,
                "system_prop_id" => $prop_info_id,
                "num" => 1,
                "lang_type" => $prop_info['lang_type'],
                "create_time" => date("Y-m-d H:i:s"),
                "update_time" => date("Y-m-d H:i:s")
            );
            DB::name("user_buycar")->insert($data);
            $msg = "加入购物车成功";
        }
        $this->success($msg);
    }

    /**
     * 修改购物车数量
     * @ApiMethod (POST)
     * @param string $prop_info_id  道具id
     * @param string $num  修改后数量
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0",description="通讯状态，0为失败，1为成功")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功",description="返回内容")
     * @ApiReturnParams   (name="data",description="扩展数据返回")
     */
    public function doEditUserBuycar()
    {
        $prop_info_id = input("prop_info_id");
        if(empty($prop_info_id)){
            $this->error("请选择道具");
        }
        $prop_info = DB::name("system_prop")->where("id",$prop_info_id)->find();
        if(empty($prop_info)){
            $this->error("道具有误");
        }
        $num = input("num");
        if(intval($num) < 1){
            $this->error("数量有误");
        }

        $user_buy_car = DB::name("user_buycar")->where(array("user_id"=>$this->auth->id,"system_prop_id"=>$prop_info_id))->find();
        if($user_buy_car){
            if($prop_info['entity_type'] == 2){
                $this->error('剧中道具限购一个');
            }
            $data = array(
                "num" => $num,
                "update_time" => date("Y-m-d H:i:s")
            );
            DB::name("user_buycar")->where("id",$user_buy_car['id'])->update($data);
            $msg = "修改购物车成功";
        }else{
            $data = array(
                "user_id" => $this->auth->id,
                "system_prop_id" => $prop_info_id,
                "num" => $num,
                "lang_type" => $prop_info['lang_type'],
                "create_time" => date("Y-m-d H:i:s"),
                "update_time" => date("Y-m-d H:i:s")
            );
            DB::name("user_buycar")->insert($data);
            $msg = "修改购物车成功";
        }
        $this->success($msg);
    }

    /**
     * 购物车商品
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0",description="通讯状态，0为失败，1为成功")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功",description="返回内容")
     * @ApiReturnParams   (name="data",description="扩展数据返回")
     * @ApiReturnParams   (name="id", type="string", required=true, sample="返回成功",description="购物车id")
     * @ApiReturnParams   (name="num", type="string", required=true, sample="返回成功",description="购物车商品数量")
     * @ApiReturnParams   (name="prop_name", type="string", required=true, sample="返回成功",description="购物车商品名称")
     * @ApiReturnParams   (name="prop_image", type="string", required=true, sample="返回成功",description="购物车商品图片")
     * @ApiReturnParams   (name="prop_blurb", type="string", required=true, sample="返回成功",description="购物车商品简介")
     * @ApiReturnParams   (name="prop_money", type="string", required=true, sample="返回成功",description="购物车商品单价")
     */
    public function getUserBuycar()
    {
        if($this->auth->id){
            $user = DB::name("user")->where("id",$this->auth->id)->find();
            $lang_type = $user['lang_type'];
        }else{
            $lang_type = 1;
        }
        $total_money = 0;
        $total_num = 0;
        $list = DB::name("user_buycar")->where(array("user_id"=>$this->auth->id,"lang_type"=>$lang_type))->order("id desc")->select();
        foreach($list as $key => $val){
            $prop = DB::name("system_prop")->where("id",$val['system_prop_id'])->find();
            $list[$key]['prop_name'] = $prop['name'];
            $list[$key]['prop_image'] = $prop['image'];
            $list[$key]['prop_blurb'] = $prop['blurb'];
            $list[$key]['prop_money'] = $prop['money'];
            $list[$key]['entity_type'] = $prop['entity_type'];
            
            $total_money += $prop['money']*$val['num'];
            $total_num += $val['num'];
        }
        $result['list'] = $list;
        $result['total']['total_money'] = $total_money;
        $result['total']['total_num'] = $total_num;
        $this->success('查询成功', $result);
    }
     /**
     * 使用兑换码
     * @ApiMethod (POST)
     * @param string $order_no  兑换码编号
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0",description="通讯状态，0为失败，1为成功")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功",description="返回内容")
     * @ApiReturnParams   (name="data",description="扩展数据返回")
     */
    public function doUserRedeemCode()
    {
        $order_no = input("order_no");
        if(empty($order_no)){
            $this->error("请输入兑换码");
        }
        $redeem_code = DB::name("redeem_code")->where(array("order_no"=>$order_no,"status"=>1))->find();
        if(empty($redeem_code)){
            $this->error("兑换码有误");
        }
        
        if($redeem_code['status'] != 1){
            $this->error("兑换码有误");
        }
        $user_redeem_code = DB::name("redeem_code")->where(array("order_no"=>$order_no,"user_id"=>$this->auth->id))->find();
        if($user_redeem_code){
            $this->error("您已使用过该兑换码");
        }
        $msg = '';
        if($redeem_code['num']){
            $user = DB::name("user")->where("id",$this->auth->id)->find();
            DB::name("user")->where("id",$this->auth->id)->setInc("money",$redeem_code['num']);
            $money_log = array(
                "user_id" => $this->auth->id,
                "money" => $redeem_code['num'],
                "before" => $user['money'],
                "after" => $user['money']+$redeem_code['num'],
                "memo" => "使用兑换码".$order_no."获得".$redeem_code['num']."嘶豆",
                "createtime" => time()
            );
            DB::name("user_money_log")->insert($money_log);
            if($msg){
                $msg .= "|"."1.1,嘶豆,,".$redeem_code['num'];
            }else{
                $msg = "1.1,嘶豆,,".$redeem_code['num'];
            }
        }
        if($redeem_code['system_prop_json']){
            $system_prop_json = json_decode($redeem_code['system_prop_json'],true);
            $prop_data = array();
            foreach($system_prop_json as $key => $val){
                $system_prop = DB::name("system_prop")->where("id",$val['id'])->find();
                // $user_bag_find = DB::name("user_bag")->where(array("user_id"=>$this->auth->id,"system_prop_id"=>$val['id']))->find();
                // if($user_bag_find){
                //     if($user_bag_find['status'] != 3){
                //         DB::name("user_bag")->where("id",$user_bag_find['id'])->setInc("num",$val['num']);
                //     }else{
                //         DB::name("user_bag")->where("id",$user_bag_find['id'])->setInc("num",$val['num']);
                //         if($user_bag_find['over_num'] == 0){
                //             DB::name("user_bag")->where("id",$user_bag_find['id'])->update(array("status"=>1));
                //         }else{
                //             DB::name("user_bag")->where("id",$user_bag_find['id'])->update(array("status"=>2));
                //         }
                //     }
                // }else{
                //     $prop_data[] = array(
                //         "user_id" => $this->auth->id,
                //         "order_no" => $order_no,
                //         "buy_type" => 1,
                //         "name" => $system_prop['name'],
                //         "system_prop_id" => $system_prop['id'],
                //         "type" => $system_prop['type'],
                //         "num" => $val['num'],
                //         "over_num" => 0,
                //         "status" => 1,
                //         "create_time" => date("Y-m-d H:i:s")
                //     );
                // }
                for($i=1;$i<=$val['num'];$i++){
                    $prop_data[] = array(
                        "user_id" => $this->auth->id,
                        "order_no" => $order_no,
                        "buy_type" => 1,
                        "name" => $system_prop['name'],
                        "system_prop_id" => $val['id'],
                        "type" => $system_prop['type'],
                        "num" => 1,
                        "over_num" => 0,
                        "status" => 1,
                        "create_time" => date("Y-m-d H:i:s")
                    );
                    if($msg){
                        $msg .= "|".$system_prop['id'].",".$system_prop['name'].",".$system_prop['image'].",".$system_prop['money'];
                    }else{
                        $msg = $system_prop['id'].",".$system_prop['name'].",".$system_prop['image'].",".$system_prop['money'];
                    }
                }
            }
            DB::name("user_bag")->insertAll($prop_data);
        }

        if($redeem_code['di_json']){
            $di_json = json_decode($redeem_code['di_json'],true);
            foreach($di_json as $key => $val){
                for($i=1;$i<=$val['num'];$i++){
                    $di_data[] = array(
                        "user_id" => $this->auth->id,
                        "name" => '抵扣券',
                        "system_prop_id" => 0,
                        "order_no" => $order_no,
                        "buy_type" => 1,
                        "type" => 2,
                        "money" => $val['money'],
                        "ratio" => 0,
                        "door" => $val['door'],
                        "num" => 1,
                        "over_num" => 0,
                        "status" => 1,
                        "create_time" => date("Y-m-d H:i:s")
                    );
                    if($msg){
                        $msg .= "|"."2.2,抵扣券,,0";
                    }else{
                        $msg = "2.2,抵扣券,,0";
                    }
                }
            }
            DB::name("user_bag")->insertAll($di_data);
        }
        
        if($redeem_code['zhe_json']){
            $zhe_json = json_decode($redeem_code['zhe_json'],true);
            foreach($zhe_json as $key => $val){
                for($i=1;$i<=$val['num'];$i++){
                    $zhe_data[] = array(
                        "user_id" => $this->auth->id,
                        "name" => '折扣券',
                        "system_prop_id" => 0,
                        "order_no" => $order_no,
                        "buy_type" => 1,
                        "type" => 3,
                        "money" => 0,
                        "ratio" => $val['ratio'],
                        "door" => $val['door'],
                        "num" => 1,
                        "over_num" => 0,
                        "status" => 1,
                        "create_time" => date("Y-m-d H:i:s")
                    );
                    if($msg){
                        $msg .= "|"."3.3,折扣券,,0";
                    }else{
                        $msg = "3.3,折扣券,,0";
                    }
                }
            }
            DB::name("user_bag")->insertAll($zhe_data);
        }

        DB::name("redeem_code")->where("id",$redeem_code['id'])->update(array("status"=>2,"user_id"=>$this->auth->id,"update_time"=>date("Y-m-d H:i:s")));

        $this->success("兑换成功",$msg);
    }

    /**
     * 购买道具
     * @ApiMethod (POST)
     * @param string $user_buycar_id  购物车id,多个以英文逗号分隔
     * @param string $user_bag_id  优惠券id，多个以英文逗号分隔
     * @param string $type  类型：1=抵扣券，2=折扣券
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0",description="通讯状态，0为失败，1为成功")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功",description="返回内容")
     * @ApiReturnParams   (name="data",description="扩展数据返回")
     */
    public function doUserBuycarOrder()
    {
        $user_buycar_id = input("user_buycar_id");
        if(empty($user_buycar_id)){
            $this->error("请选择购物车商品");
        }
        
        $user_buycar = DB::name("user_buycar")->where("id in (".$user_buycar_id.")")->select();
        // print_r($user_buycar);exit;
        if(empty($user_buycar)){
            $this->error("购物车商品有误");
        }
        $total_money = 0;
        foreach($user_buycar as $key => $val){
            $system_prop = DB::name("system_prop")->where("id",$val['system_prop_id'])->find();
            $total_money += $system_prop['money']*$val['num'];
            $system_prop_ids[] = $system_prop['id'];
        }

        $type = input("type");
        $user_bag_id = input("user_bag_id");
        $coupon_money = 0;
        if($user_bag_id){
            if(empty($type)){
                $this->error("类型不能为空");
            }
            $user_bag = DB::name("user_bag")->where("id in (".$user_bag_id.")")->select();
            if($user_bag){
                $msg = '';
                $ratio = 1;
                foreach($user_bag as $key => $val){
                    if($val['type'] == 1){
                        $msg = "优惠券类型有误";
                    }
                    if($val['type'] == $type){
                        $msg = "优惠券类型有误";
                    }
                    if($val['door'] > $total_money){
                        $msg = "优惠券有误";
                    }
                    if($type == 1){
                        $coupon_money += $val['money'];
                    }else{
                        $ratio *= $total_money*$val['ratio']/100;
                    }
                }
                if($type == 2){
                    $coupon_money = (1-$ratio)*$total_money;
                }
                
                if($msg){
                    $this->error($msg);
                }
            }
        }
        $real_money = $total_money - $coupon_money;
        $user = DB::name("user")->where("id",$this->auth->id)->find();
        if($user['money'] < $real_money){
            $this->error("余额不足");
        }
        //修改用户余额
        DB::name("user")->where("id",$this->auth->id)->setDec("money",$real_money);
        //插入记录
        $money_log = array(
            "user_id" => $this->auth->id,
            "money" => "-".$real_money,
            "before" => $user['money'],
            "after" => $user['money']-$real_money,
            "memo" => "购买道具花费".$real_money."嘶豆",
            "createtime" => time()
        );
        DB::name("user_money_log")->insert($money_log);

        //使用优惠券
        if($user_bag_id){
            $user_bag = DB::name("user_bag")->where("id in (".$user_bag_id.")")->select();
            if($user_bag){
                foreach($user_bag as $key => $val){
                    DB::name("user_bag")->where("id",$val['id'])->update(array("over_num"=>$val['num'],"status"=>2,"update_time"=>date("Y-m-d H:i:s")));
                }
            }
        }
        $order_no = date("YmdHis").random::alnum(6);

        //插入订单
        $order_data = array(
            "user_id" => $this->auth->id,
            "user_buycar_ids" => $user_buycar_id,
            "order_no" => $order_no,
            "system_prop_ids" => implode(",",$system_prop_ids),
            "total_money" => $total_money,
            "coupon_money" => $coupon_money,
            "real_money" => $real_money,
            "status" => 2,
            "create_time" => date("Y-m-d H:i:s")
        );
        DB::name("order_prop")->insert($order_data);
        //更新购物车
        $prop_data = array();
        foreach($user_buycar as $key => $val){
            $system_prop = DB::name("system_prop")->where("id",$val['system_prop_id'])->find();
            
            if($system_prop['type'] == 1){
                $system_prop_type = 1;
            }else if($system_prop['type'] == 2){
                $system_prop_type = 4;
            }else if($system_prop['type'] == 3){
                $system_prop_type = 5;
            }else if($system_prop['type'] == 4){
                $system_prop_type = 6;
            }else if($system_prop['type'] == 5){
                $system_prop_type = 2;
            }else if($system_prop['type'] == 6){
                $system_prop_type = 3;
            }else if($system_prop['type'] == 7){
                $system_prop_type = 7;
            }else if($system_prop['type'] == 8){
                $system_prop_type = 8;
            }
            
            // $user_bag_find = DB::name("user_bag")->where(array("user_id"=>$this->auth->id,"system_prop_id"=>$val['system_prop_id']))->find();
            // if($user_bag_find){
            //     if($user_bag_find['status'] != 3){
            //         DB::name("user_bag")->where("id",$user_bag_find['id'])->setInc("num",$val['num']);
            //     }else{
            //         DB::name("user_bag")->where("id",$user_bag_find['id'])->setInc("num",$val['num']);
            //         if($user_bag_find['over_num'] == 0){
            //             DB::name("user_bag")->where("id",$user_bag_find['id'])->update(array("status"=>1));
            //         }else{
            //             DB::name("user_bag")->where("id",$user_bag_find['id'])->update(array("status"=>2));
            //         }
            //     }
            // }else{
                for($i=1;$i<=$val['num'];$i++){
                    $prop_data[] = array(
                        "user_id" => $this->auth->id,
                        "order_no" => $order_no,
                        "buy_type" => 2,
                        "name" => $system_prop['name'],
                        "system_prop_id" => $system_prop['id'],
                        "type" => $system_prop_type,
                        "num" => 1,
                        "over_num" => 0,
                        "status" => 1,
                        "create_time" => date("Y-m-d H:i:s")
                    );
                }
            // }
            //删除购物车数据
            DB::name("user_buycar")->where("id",$val['id'])->delete();
        }
        if($prop_data){
            DB::name("user_bag")->insertAll($prop_data);
        }
        $this->success("购买成功");
    }
    
    
    /**
     * 看剧购买道具
     * @ApiMethod (POST)
     * @param string $system_prop_id  道具id
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0",description="通讯状态，0为失败，1为成功")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功",description="返回内容")
     * @ApiReturnParams   (name="data",description="扩展数据返回")
     */
    public function doUserPropOrder()
    {
        $system_prop_id = input("system_prop_id");
        if(empty($system_prop_id)){
            $this->error("请选择道具");
        }
        
        $system_prop = DB::name("system_prop")->where("id",$system_prop_id)->find();
        // print_r($user_buycar);exit;
        if(empty($system_prop)){
            $this->error("道具有误");
        }
        
        $user = DB::name("user")->where("id",$this->auth->id)->find();
        if($user['money'] < $system_prop['money']){
            return json(['code'=>999,'data'=>'','msg'=>'嘶豆余额不足']);
        }
        //修改用户余额
        DB::name("user")->where("id",$this->auth->id)->setDec("money",$system_prop['money']);
        //插入记录
        $money_log = array(
            "user_id" => $this->auth->id,
            "money" => "-".$system_prop['money'],
            "before" => $user['money'],
            "after" => $user['money']-$system_prop['money'],
            "memo" => "购买道具花费".$system_prop['money']."嘶豆",
            "createtime" => time()
        );
        DB::name("user_money_log")->insert($money_log);

        
        $order_no = date("YmdHis").random::alnum(6);

        //插入订单
        $order_data = array(
            "user_id" => $this->auth->id,
            "user_buycar_ids" => 0,
            "order_no" => $order_no,
            "system_prop_ids" => $system_prop_id,
            "total_money" => $system_prop['money'],
            "coupon_money" => 0,
            "real_money" => $system_prop['money'],
            "status" => 2,
            "create_time" => date("Y-m-d H:i:s")
        );
        DB::name("order_prop")->insert($order_data);
        if($system_prop['type'] == 1){
            $system_prop_type = 1;
        }else if($system_prop['type'] == 2){
            $system_prop_type = 4;
        }else if($system_prop['type'] == 3){
            $system_prop_type = 5;
        }else if($system_prop['type'] == 4){
            $system_prop_type = 6;
        }else if($system_prop['type'] == 5){
            $system_prop_type = 2;
        }else if($system_prop['type'] == 6){
            $system_prop_type = 3;
        }else if($system_prop['type'] == 7){
            $system_prop_type = 7;
        }else if($system_prop['type'] == 8){
            $system_prop_type = 8;
        }
        $prop_data = array(
            "user_id" => $this->auth->id,
            "order_no" => $order_no,
            "buy_type" => 2,
            "name" => $system_prop['name'],
            "system_prop_id" => $system_prop['id'],
            "type" => $system_prop_type,
            "num" => 1,
            "over_num" => 0,
            "status" => 1,
            "create_time" => date("Y-m-d H:i:s")
        );
        $user_bag_id = DB::name("user_bag")->insertGetId($prop_data);
        $this->success("购买成功",['user_bag_id'=>$user_bag_id]);
    }
}
